package top.wangxiaomei.ai.controller;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.wangxiaomei.ai.other.Abs;
import top.wangxiaomei.ai.other.Meaning;
import top.wangxiaomei.ai.other.TreeNode;
import top.wangxiaomei.ai.other.WordEX;
import top.wangxiaomei.ai.service.impl.ModelsServiceImpl;
import top.wangxiaomei.ai.service.impl.SenServiceImpl;
import top.wangxiaomei.ai.service.impl.WordsServiceImpl;

import java.util.*;

@RestController
@RequestMapping("/")
public class AiController {
    Gson gson = new Gson();
    @Autowired
    public WordsServiceImpl wordsService;
    @Autowired
    public ModelsServiceImpl modelsService;
    @Autowired
    public SenServiceImpl senService;
    // 定义概念
    @GetMapping("/defWord/{word}")
    public String defWord(@PathVariable("word") String word) {
        return wordsService.defWord(word);
    }

    // 定义匹配模式
    @GetMapping("/defModel/{model}")
    public String defModel(@PathVariable("model") String word) {
       return modelsService.defModel(word);
    }

    @GetMapping("/getMeaning/{sentence}")
    public String getMeaning(@PathVariable("sentence") String string) {
        // 记录时间
        long start = System.currentTimeMillis();
        // 调用词典接口
        ArrayList<Meaning> Sens = new ArrayList<>();
        TreeNode<WordEX> root = new TreeNode<>(null);
        if(false){
            getTheMeaningWay1(string,Sens);
        }else {
            getTheMeaningWay2(string, root);
        }
        // 计算时间
        long end = System.currentTimeMillis();
        System.out.println("总共耗时：" + (end - start) + "ms");
        return root.printTree(" ");
    }

    private void getTheMeaningWay2(String string, TreeNode<WordEX> root) {
        // 将 string 分词
        String[] wordList = string.split("");
        ArrayList<WordEX> words = new ArrayList<>();
        for (String word : wordList) {
            List<Abs> ex = wordsService.getEx(word);
            if (ex.isEmpty()) continue;
            ex.addFirst(new Abs(word,1));
            words.add(new WordEX(word, ex));
        }

        fun3(words,0, root);

    }

    private void fun3(ArrayList<WordEX> wordList,int lastIndex,TreeNode<WordEX> nowNode) {
        for (int i = lastIndex; i < wordList.size() ; i++) {
            if (i-1 < 0) continue;

            WordEX leftWord = wordList.get(i-1);
            WordEX rightWord = wordList.get(i);

            List<Abs> result = modelsService.machModel(leftWord, rightWord,nowNode);

            if (result.isEmpty()) continue;

            // 构建匹配树
            buildAMatchingTree(nowNode, leftWord, rightWord, result);

            ArrayList<WordEX> newWordList = getNewWordList(wordList, i,result);

            fun3(newWordList,i,nowNode);
        }
    }

    private ArrayList<WordEX> getNewWordList(ArrayList<WordEX> wordList, int i, List<Abs> result) {
        ArrayList<WordEX> newWordList = new ArrayList<>(wordList);
        newWordList.set(i-1, new WordEX(wordList.get(i-1).getWord() + wordList.get(i).getWord(), result));
        newWordList.remove(i);
        return newWordList;
    }

    private void buildAMatchingTree(TreeNode<WordEX> nowNode, WordEX leftWord, WordEX rightWord, List<Abs> result) {
        WordEX word = new WordEX(leftWord.getWord() + rightWord.getWord(), result);
        TreeNode<WordEX> parentNode = new TreeNode<>(word);
        TreeNode<WordEX> leftNode = new TreeNode<>(leftWord);
        TreeNode<WordEX> rightNode = new TreeNode<>(rightWord);
        parentNode.addChild(leftNode);
        parentNode.addChild(rightNode);

        if (nowNode.getData() != null) {
            for (TreeNode<WordEX> childNode : parentNode.getChildren()) {
                if (childNode.getData().getWord().equals(nowNode.getData().getWord())) {
                    childNode.setChildren(nowNode.getChildren());
                    break; // 找到匹配的子节点后立即退出循环
                }
            }
        }
        nowNode.setData(parentNode.getData());
        nowNode.setChildren(parentNode.getChildren());
    }
    // 获取含义方式1
    private void getTheMeaningWay1(String string, ArrayList<Meaning> Sens) {
        ArrayList<WordEX> wordList = senService.getWords(string);
        ArrayList<ArrayList<WordEX>> meanings = senService.getSens(wordList,string.length());
        for (ArrayList<WordEX> meaning : meanings) {
            senService.getMeaning(meaning,Sens);
        }
    }

}
